#include "db_installed_imformation.h"

Installed_imformation::Installed_imformation()
{
    QString db_path = "/usr/share/kylin-update-manager/original/kylin-update-manager_1.db";
    QFile file;
    if(file.exists(db_path))
    {
        //设置数据库驱动名称
        this->database=QSqlDatabase::addDatabase("QSQLITE");
        //设置数据库名
        this->database.setDatabaseName(db_path);
        //看是否能正确打开
        if(!this->database.open())
        {
            //  qDebug()<<database.lastError().text();
            qDebug()<<"打开失败";

        }
        else
        {
            qDebug()<<"数据库打开成功！";
        }
    }
}

void Installed_imformation::select()
{

    QSqlQuery sql_select;
    QString select_sql = "select * from installed order by time desc";
    sql_select.prepare(select_sql);
    if(!sql_select.exec())
    {
    qDebug()<<"select error";
    }
    else
    {
        while(sql_select.next())
        {
            appname = sql_select.value(0).toString();
            version = sql_select.value(1).toString();
            now_time = sql_select.value(2).toString();
            description = sql_select.value(3).toString();
            icon = sql_select.value(4).toString();
            statue = sql_select.value(5).toString();
        }
    }
}

bool Installed_imformation::insert(QString appname, QString version,QString description, QString icon,QString statue)
{
    QSqlQuery sql_insert;
    time_t time1 = time(NULL);
    tm *time2;
    time2 = localtime(&time1);
    time2->tm_year = time2->tm_year;
    char tmp[512] = {};
    strftime(tmp,sizeof(tmp),"%Y-%m-%d %H:%M:%S",time2);
    QString time3 = tmp;
    qDebug()<<appname<<version<<description<<icon<<statue;
    QString insert_msg = "insert into installed values(?,?,?,?,?,?)";
    sql_insert.prepare(insert_msg);
    sql_insert.addBindValue(appname);
    sql_insert.addBindValue(version);
    sql_insert.addBindValue(tmp);
    sql_insert.addBindValue(description);
    sql_insert.addBindValue(icon);
    sql_insert.addBindValue(statue);
    //bool re = sql_insert.exec(insert_msg)
    if(!sql_insert.exec())
    {
        qDebug() << "insert error";
    }
    else
    {
        qDebug() << "inserted seccuss!";
    }
    sql_insert.finish();
    database.close();
}


